package com.example.demo.controller;

import com.example.demo.bean.MultiRetrieveData;
import com.example.demo.bean.PageRetrieveData;
import com.example.demo.bean.SimpleRetrieveData;
import com.example.demo.service.RetrieveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class RetrieveController {
    @Autowired
    private RetrieveService rs;
    //简单查询
    @RequestMapping(value="Retrieve",method = RequestMethod.POST)
    public SimpleRetrieveData simpleRetreive(HttpServletRequest request){
        SimpleRetrieveData retrieveData=new SimpleRetrieveData();
        Map<String,String[]> map=request.getParameterMap();
        try {
            retrieveData.setCode(200);
            retrieveData.setData(rs.simpleRetreive(map));
            retrieveData.setMessage("success");
        }catch (Exception e){
            retrieveData.setCode(500);
            retrieveData.setMessage("Retrieve failed");
        }

        return retrieveData;
    }
    //多重查询
    @RequestMapping(value = "multiRetrieve" , method = RequestMethod.POST)
    public MultiRetrieveData multiRetrieve(HttpServletRequest request){
        MultiRetrieveData multiRetrieveData=new MultiRetrieveData();
        Map<String,String[]> map = request.getParameterMap();
        try {
            multiRetrieveData.setCode(200);
            multiRetrieveData.setData(rs.multiRetrieve(map));
            multiRetrieveData.setMessage("success");
        }catch (Exception e){
            multiRetrieveData.setCode(500);
            multiRetrieveData.setMessage("查询失败");
        }
        return multiRetrieveData;
    }
    //分页查询
    @RequestMapping(value = "pagingRetrieve" , method = RequestMethod.POST)
    public PageRetrieveData pagingRetrieve(HttpServletRequest request){
        PageRetrieveData pageRetrieveData=new PageRetrieveData();
        Map<String,String[]> map = request.getParameterMap();
        try {
            List<Map<String,Object>> resultMap=rs.pagingRetreive(map);
            int pageSize=Integer.parseInt(request.getParameter("pageSize"));
            int pageNum=Integer.parseInt(request.getParameter("pageNum"));
            int total=resultMap.size();
            int pageTotal=(int)Math.ceil(total*1.0/pageSize);
            int i=(pageNum-1)*pageSize;
            int j=i+pageSize;
            List<Map<String,Object>> resultMap2=new ArrayList<>();
            for (;i<j;i++){
                resultMap2.add(resultMap.get(i));
            }
            pageRetrieveData.setCode(200);
            pageRetrieveData.setData(resultMap2);
            pageRetrieveData.setPageTotal(pageTotal);
            pageRetrieveData.setTotal(total);
            pageRetrieveData.setMessage("查询成功");
        }catch (Exception e){
            pageRetrieveData.setCode(500);
            pageRetrieveData.setMessage("查询失败");
        }
        return pageRetrieveData;

    }

}
